package goodbaby.dklt.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.classic.core.activity.BaseActivity;
import com.classic.core.utils.StringUtil;
import com.classic.core.utils.ToastUtil;
import com.classic.okhttp.gbb.http.ActionHelp;
import com.classic.okhttp.gbb.http.ObjectCallback;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;

import goodbaby.dklt.R;

/**
 * Created by Jayden on 2016/4/27 15:50.
 *
 * @version V1.0
 * @Title: ForgetActivity
 * @Description: 忘记密码页面
 */
public class ForgetActivity extends BaseActivity {

    private String account;
    private Button btnGetCode;
    private Button btnSubmit;
    private String code;
    private EditText etAccount;
    private EditText etCode;
    private EditText etPsd;
    private EditText etSurePsd;
    private ImageView ivBack;
    private String psd;
    private String psdSure;
    private GetCodeTimeCount time;
    private TextView tvTitle;

    private void getCode() {
        this.account = this.etAccount.getText().toString().trim();
        if (StringUtil.isMobile(account)) {
            ActionHelp.gbbVerifyCode(this, account, new ObjectCallback<String>() {
                @Override
                public void onSuccess(String response) {
                    ToastUtil.showToast(ForgetActivity.this, "验证码已发送到手机");
                    time.start();
                }

                @Override
                public void onError(int code) {
                    ToastUtil.showToast(ForgetActivity.this, "验证码获取失败");
                }

                @Override
                public Type getType() {
                    return new TypeToken<String>() {
                    }.getType();
                }
            });
        } else {
            ToastUtil.showToast(this, "输入的手机号码有误");
        }
    }

    private void submit() {
        account = etAccount.getText().toString().trim();
        psd = etPsd.getText().toString().trim();
        psdSure = etSurePsd.getText().toString().trim();
        code = etCode.getText().toString().trim();
        if (StringUtil.isEmpty(account)) {
            ToastUtil.showToast(this, "手机号不能为空");
            return;
        }
        if (!StringUtil.isMobile(account)) {
            ToastUtil.showToast(this, "输入的手机号码有误");
            return;
        }
        if (StringUtil.isEmpty(psd)) {
            ToastUtil.showToast(this, "密码不能为空");
            return;
        }
        if (StringUtil.isEmpty(psdSure)) {
            ToastUtil.showToast(this, "确认密码不能为空");
            return;
        }
        if (!this.psd.equals(psdSure)) {
            ToastUtil.showToast(this, "两次密码输入不一致");
            return;
        }
        if (StringUtil.isEmpty(code)) {
            ToastUtil.showToast(this, "短信验证码不能为空");
            return;
        }
        ActionHelp.gbbFindPassword(this, this.account, this.psd, this.code, new ObjectCallback<String>() {
            public Type getType() {
                return new TypeToken<String>() {
                }.getType();
            }

            public void onError(int code) {
                ToastUtil.showToast(ForgetActivity.this, "密码重置失败");
            }

            public void onError(int paramAnonymousInt, String paramAnonymousString) {
                super.onError(paramAnonymousInt, paramAnonymousString);
                ToastUtil.showToast(ForgetActivity.this, "密码重置失败");
            }

            public void onSuccess(String paramAnonymousString) {
                ToastUtil.showToast(ForgetActivity.this, "密码重置成功");
                finish();
            }
        });
    }

    public int getLayoutResId() {
        return R.layout.activity_forget_psd;
    }

    public void initData() {
        super.initData();
        this.time = new GetCodeTimeCount(120000L, 1000L);
    }

    public void initView() {
        super.initView();
        tvTitle = (TextView) findViewById(R.id.tv_common_title);
        tvTitle.setText("重置密码");
        ivBack = (ImageView) findViewById(R.id.iv_common_back);
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(this);
        etAccount = (EditText) findViewById(R.id.et_account);
        etPsd = (EditText) findViewById(R.id.et_psd);
        etSurePsd = (EditText) findViewById(R.id.et_sure_psd);
        etCode = (EditText) findViewById(R.id.et_code);
        btnGetCode = (Button) findViewById(R.id.btn_get_code);
        btnSubmit = (Button) findViewById(R.id.btn_submit);
        btnGetCode.setOnClickListener(this);
        btnSubmit.setOnClickListener(this);
    }

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
    }

    public void viewClick(View paramView) {
        super.viewClick(paramView);
        switch (paramView.getId()) {
            default:
                break;
            case R.id.iv_common_back:
                finish();
                break;
            case R.id.btn_get_code:
                getCode();
                break;
            case R.id.btn_submit:
                submit();
                break;
        }
    }

    class GetCodeTimeCount extends CountDownTimer {


        /**
         * @param millisInFuture    The number of millis in the future from the call
         *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
         *                          is called.
         * @param countDownInterval The interval along the way to receive
         *                          {@link #onTick(long)} callbacks.
         */
        public GetCodeTimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            btnGetCode.setClickable(false);
            btnGetCode.setText(millisUntilFinished / 1000L + "s");
        }

        @Override
        public void onFinish() {
            btnGetCode.setText("获取验证码");
            btnGetCode.setClickable(true);
        }

    }
}
